package com.dubbo.consumer.quickstart;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.rpc.RpcContext;
import com.dubbo.service.IHelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
public class QuickstartConsumer {
    @Reference(interfaceClass = IHelloService.class,async = true,timeout = 10000)
    private IHelloService helloService;

    @GetMapping(value = "test")
    public void test() throws ExecutionException, InterruptedException {
        // 测试异步调用
        long beginTime = System.currentTimeMillis();

        helloService.sendMessage("哈哈");
        Future<String> sendFuture = RpcContext.getContext().getFuture();

        long sendEndTime = System.currentTimeMillis();

        String send02 = helloService.sendMessage2("嘻嘻");
        Future<String> sendFuture02 = RpcContext.getContext().getFuture();

        long sendEndTime02 = System.currentTimeMillis();

        long send1 = System.currentTimeMillis();
        sendFuture.get();
        sendFuture02.get();
        long send2 = System.currentTimeMillis();
        System.out.println("执行时间="+(send2-send1));
        System.out.println(sendFuture.get()+" , "+sendFuture02.get() +
                "，send执行时间="+(sendEndTime-beginTime)+" , send2执行时间="+(sendEndTime02-beginTime));
    }
}
